<?php

namespace app\admin\controller\aps;

use app\common\controller\Backend;

use think\Log;

/**
 * 操作工管理
 *
 * @icon fa fa-circle-o
 */
class Man extends Backend
{
    
    /**
     * Man模型对象
     * @var \app\admin\model\aps\Man
     */
    protected $model = null;

    protected $noNeedRight = ['csnlz'];

    private $devicetypelist = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\aps\Man;
        $this->view->assign("stateList", $this->model->getStateList());

        $this->devicetypelist = \app\admin\model\aps\Devicetype::all();
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$row) {
                $row['devicetype1_name'] = $this->getdevicetypename($row['devicetype1_id']);
                $row['devicetype2_name'] = $this->getdevicetypename($row['devicetype2_id']);
                $row['devicetype3_name'] = $this->getdevicetypename($row['devicetype3_id']);
                $row['devicetype4_name'] = $this->getdevicetypename($row['devicetype4_id']);
                $row['devicetype5_name'] = $this->getdevicetypename($row['devicetype5_id']);
                $row['devicetype6_name'] = $this->getdevicetypename($row['devicetype6_id']);
                $row['devicetype7_name'] = $this->getdevicetypename($row['devicetype7_id']);
                $row['devicetype8_name'] = $this->getdevicetypename($row['devicetype8_id']);
                $row['devicetype9_name'] = $this->getdevicetypename($row['devicetype9_id']);
                $row['devicetype10_name'] = $this->getdevicetypename($row['devicetype10_id']);
                $row['devicetype11_name'] = $this->getdevicetypename($row['devicetype11_id']);
                $row['devicetype12_name'] = $this->getdevicetypename($row['devicetype12_id']);
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function getdevicetypename($devicetype_id)
    {
        $devicetype_name = "";
        foreach ($this->devicetypelist as $row) {
            if ($devicetype_id == $row->id) {
                $devicetype_name = $row->name;
                break;
            }
        }
        return $devicetype_name;
    }

    //重算能力值
    public function csnlz() {
//        $list = $this->model->where('id', 'in', $ids)->select();
//
//        foreach ($list as $row) {
//            $row->procedure1_startdate = '0000-00-00 00:00:00';
//            $row->procedure2_startdate = '0000-00-00 00:00:00';
//            $row->procedure3_startdate = '0000-00-00 00:00:00';
//            $row->procedure4_startdate = '0000-00-00 00:00:00';
//            $row->procedure5_startdate = '0000-00-00 00:00:00';
//            $row->procedure6_startdate = '0000-00-00 00:00:00';
//            $row->procedure7_startdate = '0000-00-00 00:00:00';
//            $row->procedure8_startdate = '0000-00-00 00:00:00';
//
//            $row->procedure1_finishdate = '0000-00-00 00:00:00';
//            $row->procedure2_finishdate = '0000-00-00 00:00:00';
//            $row->procedure3_finishdate = '0000-00-00 00:00:00';
//            $row->procedure4_finishdate = '0000-00-00 00:00:00';
//            $row->procedure5_finishdate = '0000-00-00 00:00:00';
//            $row->procedure6_finishdate = '0000-00-00 00:00:00';
//            $row->procedure7_finishdate = '0000-00-00 00:00:00';
//            $row->procedure8_finishdate = '0000-00-00 00:00:00';
//
//            $row->finishnum = 0;
//            $row->save();
//        }

        $this->success('重算能力值OK');
    }

}
